package net.huashitong.Project.web;

import com.sirdc.modules.core.web.JsonBaseController;
import com.sirdc.modules.core.web.model.Message;
import net.huashitong.Project.entity.ProjectCollection;
import net.huashitong.Project.filter.ProjectCollectionFilter;
import net.huashitong.Project.service.ProjectCollectionService;
import net.huashitong.user.entity.BnpUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.util.WebUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 项目收藏
 */
@Controller
@RequestMapping("/web/collection")
public class ProjectCollectionWebController extends JsonBaseController {
    @Override
    protected String getView(String s) {
        return null;
    }

    @Autowired
    private ProjectCollectionService projectCollectionService;

    /**
     * 添加收藏
     *
     * @param projectId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/addCollections/{projectId}", method = RequestMethod.GET)
    public Message addProjectCollections(@PathVariable String projectId, HttpServletRequest request) {
        BnpUser bnpUser = (BnpUser) WebUtils.getSessionAttribute(request, "user");
        String userId = bnpUser.getSysId();
        //查询是否已有收藏
        List<ProjectCollection> list = projectCollectionService.queryByProjectIdAndUser(projectId, userId);
        if (CollectionUtils.isEmpty(list)) {
            ProjectCollection collection = new ProjectCollection();
            collection.setCollectFlag("1");
            collection.setCollectUser(userId);
            collection.setProjectId(projectId);
            projectCollectionService.save(collection);
        } else {
            for (ProjectCollection collection : list) {
                collection.setCollectFlag("1");
            }
            projectCollectionService.batchUpdate(list);
        }
        return coverMessage("200", "添加收藏成功！");
    }


    /**
     * 取消收藏
     * @param projectId
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/cancelCollections/{projectId}", method = RequestMethod.GET)
    public Message cancelCollections(@PathVariable String projectId,  HttpServletRequest request) {
        BnpUser bnpUser = (BnpUser) WebUtils.getSessionAttribute(request, "user");
        String userId = bnpUser.getSysId();
        //查询是否已有收藏
        List<ProjectCollection> list = projectCollectionService.queryByProjectIdAndUser(projectId, userId);
        if (!CollectionUtils.isEmpty(list)) {
            for (ProjectCollection collection : list) {
                collection.setCollectFlag("0");
            }
            projectCollectionService.batchUpdate(list);
        }
        return coverMessage("200", "取消收藏成功！");
    }
}
